<!-- 跨项目调整 -->
<template>
		<view style="width: 100%; padding-top: 20rpx;background-color: #f7f7f7;min-height: 100%;overflow: auto">
        <view style="padding: 0 20rpx;font-size: 26rpx;color: #eb4b4b">该功能仅支持对无标签ID物资进行跨项目调整（其他项目物资调拨至本项目）操作，有标签ID物资请在工程管理APP-项目查询-到货确认模块下进行操作。</view>
        <view style="display: flex;justify-content: space-around;padding:0 20rpx;margin-bottom: 20rpx" v-if="manageMode === '02'">
<!--            <u-input v-model="prjCode" type="text" :border="true" placeholder="请选择批量子项目编码" style="background-color: #FFF"/>-->
            <label class="common-list-title-ex" style="width: 35%;background-color: transparent">
                <text class="colorRed">*</text>批量子项目编码 :</label>
            <view class="footer-box__item" style="flex: 1;">
                <picker @change="changeSubPrjIdArr" :value="subPrjId"
                        :range="subPrjIdArr" range-key="PRJCODE" >
                    <input class="uni-input product-apply" :value="subPrjCode" type="text"
                           @focus="aPickerFocus" style="border:2rpx solid rgba(228, 228, 228, 1);padding: 0 12rpx">
                </picker>
            </view>
        </view>
        <view style="display: flex;justify-content: space-around;padding:0 20rpx">
          <u-input v-model="prjCode" type="text" :border="true" placeholder="请输入项目编码或项目名称" style="background-color: #FFF"/>
          <u-icon name="search" color="#dbdbdb" :size="40" style="margin-left: 10rpx" @click="search"></u-icon>
        </view>
        <view style="padding: 0 20rpx">请点击调整项目</view>
        <view style="padding: 0 20rpx">
            <view v-for="(item,index) in dataList" :key="index" style="background-color: #FFFFFF;margin-bottom: 20rpx;padding:10rpx 20rpx;border-radius: 10rpx" @click.stop="clickItem(item)">
                <view style="font-weight:bold;font-size: 30rpx ">{{item.prjName}}</view>
                <view style="color: #AFAEAC;font-size:27rpx;">项目编码：{{item.prjCode}}</view>
                <view style="color: #AFAEAC;font-size:27rpx;">可调整数量：{{item.transferNum}}</view>
            </view>
        </view>
      <u-modal ref="uModal" v-model="showModal" :show-cancel-button="true" confirm-color="#eb4b4b" :title-style="{fontSize:'40rpx',paddingTop:'20rpx'}" @confirm="confirm" @cancel="cancel" :async-close="true">
        <view class="slot-content" style="padding: 20rpx 60rpx 20rpx 40rpx">
          <view style="display: flex;justify-content: flex-end;margin-bottom: 20rpx">
            <label>可调整数量:</label>
            <input type="digit" v-model="selectItem.transferNum" :disabled="true" style="width: 60%;border: 1px solid #ddd;border-radius: 4rpx;margin-left: 20rpx;padding-left: 20rpx;background-color: #efeff0"/>
          </view>
          <view style="display: flex;justify-content: flex-end;">
            <label><text style="color: red;">*</text>调整数量:</label>
            <input type="digit" v-model="num" placeholder="" style="width: 60%;border: 1px solid #ddd;border-radius: 4rpx;margin-left: 20rpx;padding-left: 20rpx"/>
          </view>
          <view style="padding-left: 170rpx;color:#eb4b4b ">{{tooltip}}</view>
        </view>
      </u-modal>
    </view>
</template>

<script>

export default {
    data() {
        return {
            prjCode:'',
            categoryCode:'',
            prjInfo:{},
            dataList:[],
            showModal:false,
            selectItem:{},
            num:'',
            pageNum: 1,
            total:1,
            tooltip:'',
            manageMode:'',
            subPrjIdArr:[],
            subPrjId:'',
            subPrjCode:'',
        }
    },
    onLoad(option) {
        this.prjInfo = JSON.parse(decodeURIComponent(option.prjInfo));
        this.categoryCode = option.categoryCode;
        this.manageMode = option.manageMode;
        console.log(this.prjInfo,this.categoryCode)
        this.getList();
        if(this.manageMode==='02'){
            this.queryPlSonPrjCode();
        }
    },
    onReachBottom(){
        console.log(111111)
        this.getList();
    },
    methods: {
        queryPlSonPrjCode(){
            this.$axios.get('/material/api/material/transfer/queryPlSonPrjCode',{ categoryCode:this.categoryCode,prjId: this.prjInfo.prjId,}).then((res)=>{
                this.subPrjIdArr=res.data;
            })
        },
        aPickerFocus() {
            uni.hideKeyboard()
        },
        changeSubPrjIdArr(e){
            this.subPrjId=this.subPrjIdArr[e.detail.value].SUBPRJID;
            this.subPrjCode=this.subPrjIdArr[e.detail.value].PRJCODE;
        },
      search(){
          this.dataList = []
          this.pageNum = 1;
          this.total = 1;
          this.getList();
      },
      getList(){
          if(this.dataList.length < this.total){
              const params = {
                  categoryCode: this.categoryCode,
                  prjId: this.prjInfo.prjId,
                  orgId: this.prjInfo.orgId,
                  prjParam: this.prjCode,
                  queryType: '2',
                  pageNum: this.pageNum,
                  pageSize: 10,
              }
              this.$axios.post('/material/api/material/transfer/queryTransferList', params).then(res => {
                  if (res?.data?.rows?.length) {
                      this.dataList = this.dataList.concat(res.data.rows)
                      this.pageNum = this.pageNum + 1;
                      this.total = res.data.total;
                      console.log(111,this.dataList,this.pageNum,res.data.total)
                  } else {
                      this.dataList = []
                      this.pageNum = 1;
                      this.total = 1;
                      uni.showModal({
                          title: "温馨提示",
                          content: "没有数据",
                          showCancel: false,
                          confirmColor:'#eb4b4b',
                      });
                  }
              })
          }
      },
      clickItem(item){
        console.log(item)
        this.showModal=true;
        this.selectItem=JSON.parse(JSON.stringify(item))
      },
      confirm(){
          this.$refs.uModal.clearLoading();
          console.log(this.num)
          if(!this.num){
              this.tooltip='请输入调整数量'
          }else if(this.num<=0){
              this.tooltip='绑定数量必须大于0，请重新输入'
          }else if(+this.num > +this.selectItem.transferNum){
              this.tooltip='填写的调整数量已超过可调整数量，请重新输入'
          }else{
              this.tooltip='';
              this.confirmCheckOne()
          }
      },
    confirmCheckOne(){
        if(this.manageMode === '02'){
            if(this.subPrjId){
                this.confirmCheckCode()
            }else{
                uni.showModal({
                    title: "温馨提示",
                    content: "请选择批量子项目编码",
                    showCancel: false,
                    confirmColor:'#eb4b4b',
                });
            }
        }else{
            this.confirmCheckCode()
        }
    },
      confirmCheckCode(){
          this.$axios.post('/material/api/material/transfer/getRetreatSpuUnitAccByMaterialCode?retreatAmount='+this.num+'&materialCode='+this.categoryCode).then(() => {
              this.checkNeedAdjust();
          })
      },
      jumpAdjustProject(params){
          let that=this;
          uni.showModal({
              title: "温馨提示",
              content: "发起跨项目调整操作需双方项目经理审批",
              confirmColor:'#eb4b4b',
              success: function (result) {
                  if(result.confirm){
                      that.$axios.post('/material/api/flows/adjustPrjFlow/startAdjustPrjFlow',params).then((res)=>{
                          uni.navigateTo({
                              url:'/inspur/adjustProject/index?workItemId='+ res.data+'&type=todo&status=1',
                          })
                      })
                  }else{
                      console.log('取消了')
                  }
              }
          });
      },
      checkNeedAdjust(){
          const params={
              oldPrjId: this.selectItem.prjId,
              newPrjId: this.prjInfo.prjId,
              categoryCode: this.categoryCode,
              transferNum: this.num,
              subPrjId:this.subPrjId,
              assetLabelId:''
          }
          this.$axios.post('/material/api/flows/adjustPrjFlow/checkIsNeedAdjustPrjFlow',params).then((res)=>{
              if(+res.data === 1){
                  this.jumpAdjustProject(params);
              }else{
                  const params1={
                      oldPrjId: this.selectItem.prjId,
                      newPrjId: this.prjInfo.prjId,
                      categoryCode: this.categoryCode,
                      transferNum: this.num,
                      subPrjId:this.subPrjId,
                  }
                  this.$axios.post('/material/api/material/transfer/doSaveMaterialTransferInfo',params1).then(()=>{
                      uni.showModal({
                          title: "温馨提示",
                          content: "操作成功",
                          showCancel: false,
                          confirmColor:'#eb4b4b',
                          success: function () {
                              uni.navigateBack({
                                  delta: 1
                              })
                          }
                      });
                  })
              }
          })
      },
      cancel(){
        console.log('取消')
        this.showModal=false;
        this.tooltip='';
        this.num='';
      }
    }
}
</script>


<style>
@import '@/common/common-ui.css';
.uni-page-body {
    background-color: #333333;
}
</style>
